using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Trippy.Animation;
using Trippy.CollidableGameComponents.Agents;
using Trippy.Levels;

namespace Trippy.CollidableGameComponents.Objects
{
    /// <summary>
    /// The base unit for 
    /// </summary>
    public class Block : CollidableGameComponents.CollidableGameComponent
    {
        public static Texture2D SpriteSheet { get; private set; }


        protected override void LoadContent()
        {
            if (SpriteSheet == null) SpriteSheet = CurrentLevel.Game.Content.Load<Texture2D>("Sprites/Objects/marioblock");
            Sprite = new AnimatedSprite(this, SpriteSheet, SpriteSheet.Height, SpriteSheet.Width, 0, 0, 0, 0, 1, 0f);
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            StandardDraw(gameTime);
            base.Draw(gameTime);
        }

        public Block(Level level)
            : base (level)
        {
            
        }
        
        public void Bounce()
        {
            Velocity = new Vector2(Velocity.X * Elasticity, Velocity.Y * -Elasticity);
        }

        protected override void CollisionHandler(CollidableGameComponents.CollidableGameComponent component, CollidableGameComponents.CollidableGameComponent.CollisionSide side)
        {
            if (component is Agent)
            {
                Agent a = (Agent)component;
                if ((a is Hero || a is Enemy) && Acceleration.Y != 0f)
                {
                    switch (side)
                    {
                        case CollisionSide.Top:
                            a.HitGround(BoundingBox.Top + Velocity.Y, Velocity);
                            break;
                        case CollisionSide.Right:
                            a.HitLeftWall(BoundingBox.Right);
                            break;
                        case CollisionSide.Bottom:
                            a.HitCeiling(BoundingBox.Bottom);
                            break;
                        case CollisionSide.Left:
                            a.HitRightWall(BoundingBox.Left);
                            break;
                        default:
                            throw new ArgumentOutOfRangeException("side");
                    }
                }
            }

            base.CollisionHandler(component, side);
        }

        public override void Update(GameTime gameTime)
        {
            UpdateStandardPhysics(TrippyUtilities.CalculateElapsedTime(gameTime));
            base.Update(gameTime);
        }


    }
}
